{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This example shows how to perform sentiment analysis on YouTube comments using AgentQL with the [Google Colaboratory](https://colab.research.google.com/) environment."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install [AgentQL](https://pypi.org/project/agentql/) library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install agentql"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Install the Playwright dependency required by AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!playwright install chromium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can [store](https://medium.com/@parthdasawant/how-to-use-secrets-in-google-colab-450c38e3ec75) keys in Google Colab's secrets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from google.colab import userdata\n",
    "\n",
    "os.environ[\"AGENTQL_API_KEY\"]=userdata.get('AGENTQL_API_KEY')\n",
    "os.environ[\"OPENAI_API_KEY\"]=userdata.get('OPENAI_API_KEY')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run AgentQL script. Unfortunately, you can't see the browser window directly in Google Colab like you would on your local machine. However, let's still interact with it and take a screen recording of the browser session to see what’s happening. \n",
    "\n",
    "Please note that an online environment like Google Colab supports **asynchronous version** of AgentQL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import agentql\n",
    "from openai import OpenAI\n",
    "from playwright.async_api import async_playwright\n",
    "from IPython.display import HTML\n",
    "from base64 import b64encode\n",
    "\n",
    "URL = \"https://www.youtube.com/watch?v=JfM1mr2bCuk\"\n",
    "\n",
    "# Define the queries to interact with the page\n",
    "QUERY = \"\"\"\n",
    "{\n",
    "    video_title\n",
    "    video_channel\n",
    "    comments[] {\n",
    "        comment_text\n",
    "        author\n",
    "    }\n",
    "}\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "async def get_comments():\n",
    "    async with async_playwright() as playwright, await playwright.chromium.launch() as browser:\n",
    "        # Set up the video recording\n",
    "        context = await browser.new_context(\n",
    "          record_video_dir=\"videos/\",\n",
    "          record_video_size={\"width\": 1280, \"height\": 720}\n",
    "        )\n",
    "\n",
    "        page = await agentql.wrap_async(await context.new_page())\n",
    "\n",
    "        await page.goto(URL)\n",
    "\n",
    "\n",
    "        for _ in range(5):\n",
    "            # Wait for the page to load (helps to load the comments on the video)\n",
    "            await page.wait_for_page_ready_state()\n",
    "\n",
    "            # Scroll down the page to load more comments\n",
    "            await page.keyboard.press(\"PageDown\")\n",
    "\n",
    "        # Use query_data() method to fetch the comments from the page\n",
    "        response = await page.query_data(QUERY)\n",
    "\n",
    "        return response\n",
    "\n",
    "\n",
    "async def perform_sentiment_analysis(comments):\n",
    "    USER_MESSAGE = \"These are the comments on the video. I am trying to understand the sentiment of the comments.\"\n",
    "\n",
    "    for comment in comments[\"comments\"]:\n",
    "        USER_MESSAGE += comment[\"comment_text\"]\n",
    "\n",
    "    SYSTEM_MESSAGE = \"\"\"You are an expert in understanding the social media analytics and analysis and specialize in analyzing sentiment of the comments.\n",
    "    Please find the comments on the video as follows:\n",
    "\n",
    "    \"\"\"\n",
    "\n",
    "    USER_MESSAGE += \"Could you please provide a summary of the comments on the video. Additionaly, just give only 3 takeaways which would be important for me as the creator of the video.\"\n",
    "\n",
    "    client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
    "\n",
    "    completion = await client.chat.completions.create(\n",
    "        model=\"gpt-3.5-turbo\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": SYSTEM_MESSAGE},\n",
    "            {\"role\": \"user\", \"content\": USER_MESSAGE},\n",
    "        ],\n",
    "    )\n",
    "\n",
    "    return completion.choices[0].message.content\n",
    "\n",
    "\n",
    "comments = await get_comments()\n",
    "summary = await perform_sentiment_analysis(comments)\n",
    "print(summary)\n",
    "\n",
    "# Display the video\n",
    "video_dir = os.path.abspath(\"videos\")\n",
    "video_files = [f for f in os.listdir(video_dir) if f.endswith('.webm')]\n",
    "if video_files:\n",
    "    video_path = os.path.join(video_dir, video_files[0])\n",
    "    with open(video_path, 'rb') as f:\n",
    "      video_bytes = f.read()\n",
    "\n",
    "    video_b64 = b64encode(video_bytes).decode('utf-8')\n",
    "    video_html = f\"\"\"\n",
    "    <video width=\"800\" controls>\n",
    "      <source src=\"data:video/webm;base64,{video_b64}\" type=\"video/webm\">\n",
    "      Your browser does not support the video tag.\n",
    "    </video>\n",
    "    \"\"\"\n",
    "    display(HTML(video_html))\n",
    "else:\n",
    "    print(\"No video file was created\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
